/*
 *  Class name: EntityDTOTransformer.java
 *  Version: 1.0
 *  Date: 31.10.2013
 */
package cz.muni.fi.pa165.soccergamesarchive.utils;

import java.util.ArrayList;
import java.util.List;
import org.dozer.MappingException;

import static cz.muni.fi.pa165.soccergamesarchive.utils.DozerBeanMapperSingletonWrapper.getInstance;

/**
 * Class make bidirectional transformation between entities (entities package)
 * and
 * DTOs (dtos package)
 *
 * @author Michal Ďuriš
 */
public class EntityDTOTransformer {

    public static <T> T transform(Object sourceObject, Class<T> destinationClass)
            throws MappingException {

        if (sourceObject == null) {
            return null;
        }

        return getInstance().map(sourceObject,
                destinationClass);
    }

    public static <T> List<T> transformList(Object sourceList,
                                            Class<T> destinationClass) {
        List<Object> sourceObjectList;
        List<T> destinationObjectList = new ArrayList<>();

        if (sourceList instanceof List) {
            sourceObjectList = (List<Object>) sourceList;
        } else {
            return null;
        }

        for (Object object : sourceObjectList) {
            destinationObjectList.add(
                    getInstance().map(object,
                            destinationClass));
        }

        return destinationObjectList;
    }

}
